package com.abbas.smartCity.ui.parkingWhere;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;

import com.abbas.smartCity.R;
import com.abbas.smartCity.adapter.ListCommAdapter;
import com.abbas.smartCity.base.BaseActivity;
import com.abbas.smartCity.bean.BaseBean;
import com.abbas.smartCity.bean.parkingWhereBean.ParkingAdvList;
import com.abbas.smartCity.retroft.ICallback;
import com.abbas.smartCity.view.MListView;
import com.google.android.material.textfield.TextInputLayout;

import java.util.Map;
import java.util.Objects;

import retrofit2.Call;

public class ParkingAdvActivity extends BaseActivity {

    private TextInputLayout til;
    private Button post;
    private Button history;
    private MListView lv;
    private ListCommAdapter<ParkingAdvList.RowsBean, ListCommAdapter.ParkingHistoryHolder> listAdapter;


    @Override
    protected int getLayoutId() {
        return R.layout.activity_adv_parking;
    }

    @Override
    protected String setCustomTitle() {
        return "意见反馈";
    }

    @Override
    protected void initView() {
        til = (TextInputLayout) findViewById(R.id.til);
        post = (Button) findViewById(R.id.post);
        history = (Button) findViewById(R.id.history);
        lv = (MListView) findViewById(R.id.lv);
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initEvent() {
        post.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final Map<String, Object> map = getMap();
                map.put("content", Objects.requireNonNull(til.getEditText()).getText().toString());
                api.postParkingAvd(createRequestBody(map)).enqueue(new ICallback<BaseBean>() {
                    @Override
                    public void onSuccess(Call<BaseBean> call, BaseBean data) {
                        showToast(data.getMsg());
                        if (listAdapter != null){
                            getHistoryAdv();
                        }
                    }
                });
            }
        });

        history.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getHistoryAdv();
            }
        });
    }

    private void getHistoryAdv(){
        api.getParkingAdvList().enqueue(new ICallback<ParkingAdvList>() {
            @Override
            public void onSuccess(Call<ParkingAdvList> call, ParkingAdvList data) {
                if (data.getRows().size() > 0) {
                    if (listAdapter == null) {
                        listAdapter = new ListCommAdapter<ParkingAdvList.RowsBean, ListCommAdapter.ParkingHistoryHolder>(
                                data.getRows(),
                                ListCommAdapter.ParkingHistoryHolder.class,
                                R.layout.parking_history_item
                        ) {
                            @Override
                            protected void show(ParkingAdvList.RowsBean item, View convertView, ParkingHistoryHolder viewHolder, int position, Context context) {
                                if (TextUtils.isEmpty(item.getTitle())) {
                                    viewHolder.name.setText("未命名意见标题");
                                } else {
                                    viewHolder.name.setText(item.getTitle());
                                }

                                if (TextUtils.isEmpty(item.getContent())) {
                                    viewHolder.tag1.setText("未编写意见内容");
                                } else {
                                    viewHolder.tag1.setText(item.getContent());
                                }

                                viewHolder.tag2.setVisibility(View.GONE);
                                viewHolder.tag3.setVisibility(View.GONE);
                                viewHolder.money.setVisibility(View.GONE);
                            }
                        };
                        lv.setAdapter(listAdapter);
                    } else {
                        listAdapter.resetData(data.getRows());
                    }
                } else {
                    showToast("暂时没有历史意见！");
                }
            }
        });
    }
}